package front

import (
	"net/http"
	"strconv"
	"time"
	"tmall_go/models"

	"github.com/gin-gonic/gin"
)

type IndexController struct{}

// func (con IndexController) TestFun(c *gin.Context) {
// 	userName, _ := c.Get("testKey")
// 	curName := "暂无用户"
// 	// 类型断言
// 	v, ok := userName.(string)
// 	if ok {
// 		curName = v
// 	}
// 	c.HTML(http.StatusOK, "frontdesk/index.html", gin.H{
// 		"title": "前台首页-" + curName,
// 		"date":  1753086904,
// 	})
// }

func (con IndexController) Index(c *gin.Context) {
	currentTime := time.Now().Format("2006-01-02 15:04:05")
	barStr := c.Query("bar")
	if barStr == "" {
		barStr = "-1"
	}
	barNum, _ := strconv.Atoi(barStr)
	goodsList := []models.Goods{}
	if barNum == -1 {
		models.DB.Find(&goodsList)
	} else {
		models.DB.Where("category=?", barNum).Find(&goodsList)
	}
	c.HTML(http.StatusOK, "frontdesk/index.html", gin.H{
		"barKey":      barNum,
		"currentTime": currentTime,
		"info":        goodsList,
	})
}
